Skip to main content

Dot Product between Two Vectors

This is an example of how to use the csdl.dot() function to compute the dot product between two vectors.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleVectorVector(Model):
    def define(self):
        m = 3
        # Shape of the vectors        vec_shape = (m, )
        # Values for the two vectors        vec1 = np.arange(m)        vec2 = np.arange(m, 2 * m)
        # Adding the vectors to csdl        vec1 = self.declare_variable('vec1', val=vec1)        vec2 = self.declare_variable('vec2', val=vec2)
        # Vector-Vector Dot Product        self.register_output('VecVecDot', csdl.dot(vec1, vec2))

sim = Simulator(ExampleVectorVector())sim.run()
print('vec1', sim['vec1'].shape)print(sim['vec1'])print('vec2', sim['vec2'].shape)print(sim['vec2'])print('VecVecDot', sim['VecVecDot'].shape)print(sim['VecVecDot'])
[0. 1. 2.]vec2 (3,)[3. 4. 5.]VecVecDot (1,)[14.]